package com.main.sales.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.main.sales.common.R;
import com.main.sales.pojo.ShoppingCart;
import com.main.sales.service.ShoppingCartService;
import com.main.sales.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    //    http://localhost:8080/shoppingCart/list
    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 加物品到购物车
     *
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //查询当前菜品或套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());//用户

        if (shoppingCart.getDishId() != null) {//添加到购物车是菜品
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {//添加到购物车是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);//查找


        if (one != null) {//菜品或套餐已存在加一
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);//更新
        } else {//菜品或套餐不存在减一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        log.info("加物品到购物车{}", shoppingCart.toString());

        return R.success(one);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        Long userId = BaseContext.getCurrentId();//当前用户
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    //http://localhost:8080/shoppingCart/clean
    @DeleteMapping("/clean")
    public R<String> clean() {
        Long userId = BaseContext.getCurrentId();//当前用户id
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(lqw);
        return R.success("清空购物车");
    }

    /**
     * 商品减一
     *
     * @param shoppingCart
     * @return
     */
    //http://localhost:8080/shoppingCart/sub
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);

        if (shoppingCart.getDishId() != null) {//购物车是菜品
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {//购物车是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);//查找
        Integer number = one.getNumber();//当前商品数量
        if (number == 1) {//数量为一，再次减就清空
            LambdaQueryWrapper<ShoppingCart> delete = new LambdaQueryWrapper<>();
            if (shoppingCart.getDishId() != null) {//菜品
                delete.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            } else {//套餐
                delete.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            }
            shoppingCartService.remove(delete);
            one.setNumber(0);//清空
        } else {
            one.setNumber(number - 1);//减一
            shoppingCartService.updateById(one);
        }

        return R.success(one);
    }
}
